package com.explorati.LeetCode110.BalancedBinaryTree;

/**
 * 110. Balanced Binary Tree
 * 
 * 平衡二叉树计算平衡因子
 * 
 * @author explorati
 *
 */
public class Solution {
	public class TreeNode {
		int val;
		TreeNode left;
		TreeNode right;

		TreeNode(int x) {
			val = x;
		}
	}

	boolean result = true;

	public boolean isBalanced(TreeNode root) {
		maxDepth(root);
		return result;
	}

	public int maxDepth(TreeNode node) {
		if (node == null) {
			return 0;
		}
		int l = maxDepth(node.left);
		int r = maxDepth(node.right);

		if (Math.abs(r - l) > 1) {
			result = false;
		}
		return 1 + Math.max(l, r);

	}
}
